package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import model.daos.PlayerDao;
import model.daos.TeamDao;
import model.daos.UmpireDao;
import model.dtos.Team;

import views.windows.LoadDataPanel;

public class LoadDataController extends LoadDataPanel
{
	private static final long serialVersionUID = 1L;
	Team selectedTeam;
	TeamDao teamDao;
	UmpireDao umpireDao;
	PlayerDao playerDao;
	File fileTeams;
	File fileUmpires;
	File filePlayers;
	
	public LoadDataController()
	{
		super();
		
		teamDao = new TeamDao();
		umpireDao = new UmpireDao();
		playerDao = new PlayerDao();
		
		fillTeams();
		addActions();
	}
	
	public void addActions()
	{		
		getEquiposButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser chooser = new JFileChooser();
				FileNameExtensionFilter fileNameExtensionFilter = new FileNameExtensionFilter("TXT & CSV", "txt", "csv");
				chooser.setFileFilter(fileNameExtensionFilter);
				chooser.showOpenDialog(LoadDataController.this);
				
				if (chooser.getSelectedFile() != null)
				{
					filePlayers = chooser.getSelectedFile();
					getEquiposTextField().setText(filePlayers.getName());
				}
			}
		});
		
		getLEquiposButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if (filePlayers != null)
				{
					try
					{
						teamDao.loadTeamsFromFile(filePlayers);
					} catch (IOException e1)
					{
						e1.printStackTrace();
					} catch (ParseException e1)
					{
						e1.printStackTrace();
					}
					
					fillTeams();
					showMessage("Equipos registrados con exito");
				}
				else
				{
					showMessage("Archivo incorrecto");
				}
			}
		});
		
		getUmpiresButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser chooser = new JFileChooser();
				FileNameExtensionFilter fileNameExtensionFilter = new FileNameExtensionFilter("TXT & CSV", "txt", "csv");
				chooser.setFileFilter(fileNameExtensionFilter);
				chooser.showOpenDialog(LoadDataController.this);
				
				if (chooser.getSelectedFile() != null)
				{
					fileUmpires = chooser.getSelectedFile();
					getUmpiresTextField().setText(fileUmpires.getName());
				}
			}
		});
		
		getLUmpiresButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if (fileUmpires != null)
				{
					try
					{
						umpireDao.loadUmpiresFromFile(fileUmpires);
					} catch (IOException e1)
					{
						e1.printStackTrace();
					} catch (ParseException e1)
					{
						e1.printStackTrace();
					}
					
					showMessage("Umpires registrados con exito");
				}
				else
				{
					showMessage("Archivo incorrecto");
				}
			}
		});
		
		getJugadoresButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser chooser = new JFileChooser();
				FileNameExtensionFilter fileNameExtensionFilter = new FileNameExtensionFilter("TXT & CSV", "txt", "csv");
				chooser.setFileFilter(fileNameExtensionFilter);
				chooser.showOpenDialog(LoadDataController.this);
				
				if (chooser.getSelectedFile() != null)
				{
					filePlayers = chooser.getSelectedFile();
					getJugadoresTextField().setText(filePlayers.getName());
				}
			}
		});
		
		getLJugadoresButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if (filePlayers != null)
				{
					try
					{
						playerDao.loadPlayersFromFile(selectedTeam.getCode(), filePlayers);
					} catch (IOException e1)
					{
						e1.printStackTrace();
					} catch (ParseException e1)
					{
						e1.printStackTrace();
					}
					
					showMessage("Jugadores registrados con exito");
				}
				else
				{
					showMessage("Archivo incorrecto");
				}
			}
		});
		
		getEquiposDisponiblesComboBox().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				selectedTeam = (Team)getEquiposDisponiblesComboBox().getSelectedItem();
			}
		});
	}
	
	void fillTeams()
	{
		DefaultComboBoxModel model = new DefaultComboBoxModel(teamDao.findAll().toArray());
				
		getEquiposDisponiblesComboBox().removeAllItems();
		getEquiposDisponiblesComboBox().setModel(model);
	}
}
